
    var ul2;
    var li;
    var cities2;
    var div2;
    var city2;
    var xmlhttp=new XMLHttpRequest();
    var selected_city2;
    var selected_li2 = -1;

function createtags2()
{

    ul2 = document.createElement('ul');
    ul2.setAttribute('id', 'list2');
    document.tripform.tocountry.disabled = true;
    city2 =  document.getElementById('tocit');
    div2 = document.getElementById('to_livesearch');
    div2.style.left = city2.offsetLeft +'px';
    div2.style.top = city2.offsetTop + city2.offsetHeight +'px';
    div2.style.width = city2.offsetWidth +'px';
    div2.style.visibility = 'hidden';
    document.tripform.tocountry.value = '';

}

function showResult2(str)
{
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4){
        if(str==xmlhttp.responseText)
                           {
                               handleSelect2(str);
                           }

        document.tripform.tocountry.value = '';
        if (str.length >= 2 && xmlhttp.responseText !=cities2){
            selected_li2 = -1;
               var mySplitResult = xmlhttp.responseText.split("/");

                   var limit = ul2.children.length;
                        for(k = 0; k < limit; k++){
                            ul2.removeChild(ul2.children[0]);
                        };

                   for(i = 0; i < mySplitResult.length; i++){

                       li = document.createElement('li');
                       li.setAttribute('onclick', 'handleSelect2(this.innerHTML)');
                       li.innerHTML = mySplitResult[i];
                       ul2.appendChild(li);
                   };
               div2.appendChild(ul2);
               cities2 = xmlhttp.responseText;
         };
         if(xmlhttp.responseText.length == 0){
               div2.style.visibility = 'hidden';
         }
         else
               div2.style.visibility = 'visible';


    };

    if (str.length < 2)
             {
                 document.tripform.tocountry.disabled = true;
                 div2.style.visibility = 'hidden';
             }
    if (xmlhttp.responseText.length == 0 && str.length >= 2)
             {
                 document.tripform.tocountry.disabled = false;
             }
     if (xmlhttp.responseText.length != 0)
             {
                 document.tripform.tocountry.disabled = true;
             }

 }

        xmlhttp.open("GET","liveSearch.php?query="+str+"&getCountry=0",true);
        xmlhttp.send();


}

function showCountry2(selected_city2)
{

      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4){
            //alert(xmlhttp.responseText);
            document.tripform.tocountry.value = xmlhttp.responseText;
            document.tripform.tocountry.disabled = true;
        }
      }

      xmlhttp.open("GET","liveSearch.php?query="+selected_city2+"&getCountry=1",true);
      xmlhttp.send();


}


function searchCountry2(possible_city)
{
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4){
            //alert(xmlhttp.responseText);
            if(xmlhttp.responseText.length == 0)
            document.tripform.tocountry.disabled = false;
            else{
                document.tripform.tocountry.disabled = true;
                document.tripform.tocountry.value = xmlhttp.responseText;
            }

        }
      }

      xmlhttp.open("GET","liveSearch.php?query="+possible_city+"&getCountry=1",true);
      xmlhttp.send();
}


function handleSelect2(text)
{
    document.tripform.tocity.value = text.toString();
    showCountry2(text.toString());
    div2.style.visibility = 'hidden';
    document.tripform.tocountry.disabled = true;
}

function handleArrows2(e)
{

        switch(e.keyCode)
        {
        case 40:
            if(selected_li2 == -1 || selected_li2 == document.getElementById('list2').children.length-1){
                handleLis2(selected_li2,0);
            }
            else{
                handleLis2(selected_li2,++selected_li2);
            }
            break;
        case 38:
            if(selected_li2 == 0){
                handleLis2(selected_li2,document.getElementById('list2').children.length-1);
            }
            else
                handleLis2(selected_li2,--selected_li2);
            break;
        case 39:
            handleSelect2(document.getElementById('list2').children[selected_li2].innerHTML);
            handleLis2(selected_li2,-1);
            break;
        case 13:
            handleSelect2(document.getElementById('list2').children[selected_li2].innerHTML);
            handleLis2(selected_li2,-1);
            break;
        }


}

function handleLis2(previous,current){
    if(previous != -1){
        document.getElementById('list2').children[previous].style.color = "black";
        document.getElementById('list2').children[previous].style.backgroundColor = "white";
    }
    if(current != -1){
        document.getElementById('list2').children[current].style.color = "white";
        document.getElementById('list2').children[current].style.backgroundColor = "grey";
    }
    selected_li2 = current;
}

function disableEnter2(e){
     return (e.keyCode != 13);
}

function hideDiv2()
{
     div2.style.visibility = 'hidden';
}

